package com.ruoyi.project.hcy.saleManagement.controller;

import com.ruoyi.common.constant.checkNameUniqueContent;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hcy.saleManagement.domain.*;
import com.ruoyi.project.hcy.saleManagement.mapper.OrderManagerMapper;
import com.ruoyi.project.hcy.saleManagement.service.IOrderManagerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/hcy/saleManagement")
@Controller
public class OrderManagerController extends BaseController {

    private String prefix = "hcy/saleManagement/orderManger";

    @Autowired
    private IOrderManagerService OrderManagerService;


    @GetMapping("/orderManger")
    @RequiresPermissions("hcy:orderManger:view")
    public String viewFun() {
        System.out.println("waw");
        return prefix + "/orderManger";
    }

    /*
     * 第一次载入查询
     * 模糊查询：根据项目名称查询
     * */
    @PostMapping("/list")
    @RequiresPermissions("hcy:orderManger:view")
    @ResponseBody
    public TableDataInfo listFun(Project project) {
        startPage();
        List<ProjectVo> list = OrderManagerService.listFun(project);
        return getDataTable(list);
    }

    /*
     *添加页面跳转
     * 数据加载
     * */
    @RequiresPermissions("hcy:orderManger:add")
    @GetMapping("/add")
    public String addFun(Model model) {
        List<Order> list = OrderManagerService.selectProjectTypeName();
        model.addAttribute("vos", list);
        ArrayList<String> shcdule = getShcdule();
        model.addAttribute("deptName", OrderManagerService.getDeptName());
        model.addAttribute("shcdule", shcdule);
        return prefix + "/add";
    }

    /*获取排序*/
    private ArrayList<String> getShcdule() {
        ArrayList<String> shcdule = new ArrayList<>();
        for (int i = 0; i < 11; i++) {
            shcdule.add(String.valueOf((i * 10)));
        }
        return shcdule;
    }


    /*
    字段验证
    * */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(Vo vo) {
        System.out.println(vo);
        String s = OrderManagerService.checkTypeNameUnique(vo);
        return s;
    }

    @RequiresPermissions("hcy:orderManger:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSaveFun(Client client, Schedule schedule, ProjectVo projectVo, String crewName) {

        /*设置日期的字符串*/
        String s = getTimeFormat(projectVo, "yyyy-MM-dd HH:mm:ss");
        projectVo.setLocaldateTimeString(s);
        System.out.println(client);
        System.out.println(schedule);
        System.out.println(projectVo);
        System.out.println(projectVo.getLocaldateTimeString());
        System.out.println("**************************");
        return toAjax(OrderManagerService.addSaveFun(client, projectVo, schedule, crewName));

    }


    @PostMapping("/checkCrewName")
    @ResponseBody
    public String checkCrewName(String crewName, String deptName) {
        System.out.println(crewName);
        String result = OrderManagerService.checkCrewName(crewName, deptName);
        return result;
    }

    //    checkdeptPrincipal
    @PostMapping("/checkdeptPrincipal")
    @ResponseBody
    public String checkdeptPrincipal(String deptPrincipal, String deptName) {
        System.out.println(deptPrincipal);
        String result = OrderManagerService.checkdeptPrincipal(deptPrincipal, deptName);
        return result;
    }


    /*edit页面跳转*/
    @GetMapping("/edit/{id}")
    @RequiresPermissions("hcy:orderManger:edit")
    public String edit(@PathVariable int id, Model model) {
        /*
         * 通过传入project的ID，查询project的信息
         *
         * 2.通过查询的project的信息，通过project的ClientID查询到用户信息
         * 3.通过project的ID，去schedule表查询，查询所有schedule的信息
         * 4.通过project的Crew_ID去查询用户，并得到他的部门
         * */
        model.addAttribute("deptName", OrderManagerService.getDeptName());
        projectVo = OrderManagerService.selectProjectWithCrewAndClientAndSchedule(id);
        model.addAttribute("projectVo", projectVo);
        /*获取Order的订单类型*/
        model.addAttribute("vos", OrderManagerService.selectProjectTypeName());
        /*百分比进度加载*/
        model.addAttribute("shcdule", getShcdule());
        System.out.println(id);
        System.out.println(projectVo.getProjectRemark());
        return prefix + "/edit";
    }

    /*设置temp值，给用修改回显判断*/
    private ProjectVo projectVo;

    /*修改*/
    @PostMapping("/EditcheckTypeNameUnique")
    @ResponseBody
    public String EditcheckTypeNameUnique(Vo vo) {
        if ((vo.getProjectTitle() != null && vo.getProjectTitle().equals(projectVo.getProjectTitle())) ||
                (vo.getScheduleId() != null && vo.getScheduleId().equals(String.valueOf(projectVo.getScheduleId()))) ||
                (vo.getClientId() != null && vo.getClientId().equals(String.valueOf(projectVo.getClientId()))) ||
                (vo.getProjectId() != null && vo.getProjectId().equals(String.valueOf(projectVo.getProjectId()))) ||
                (vo.getCrewName() != null && vo.getCrewName().equals(projectVo.getCrewName())) ||
                (vo.getDeptPrincipal() != null && vo.getDeptPrincipal().equals(projectVo.getDeptPrincipal()))
        ) {
            return checkNameUniqueContent.TYPE_NAME_UNIQUE;
        } else {
            return checkTypeNameUnique(vo);
        }
    }

    //    EditcheckCrewNameOrDeptPrincipal
    @PostMapping("/EditcheckCrewNameOrDeptPrincipal")
    @ResponseBody
    public String EditcheckCrewNameOrDeptPrincipal(String crewName, String deptPrincipal, String deptName) {

        System.out.println(deptPrincipal);

        if (crewName != null && projectVo.getCrewName() != null) {
            if (!projectVo.getCrewName().equals(crewName)) {//false 不等于原来的crewName
                return OrderManagerService.checkCrewName(crewName, deptName);
            } else {
                return checkNameUniqueContent.TYPE_NAME_UNIQUE;//放行
            }
        }

        else if (deptPrincipal != null && projectVo.getDeptPrincipal() != null) {
            if (!projectVo.getDeptPrincipal().equals(deptPrincipal)) {//false 不等于原来的deptPrincipal
                return OrderManagerService.checkdeptPrincipal(deptPrincipal, deptName);
            } else {
                return checkNameUniqueContent.TYPE_NAME_UNIQUE;//放行
            }
        }
        else {
            if (deptPrincipal!= null && deptName!=null){
                return OrderManagerService.checkdeptPrincipal(deptPrincipal, deptName);
            }else if (crewName != null && deptName!=null){
                return OrderManagerService.checkCrewName(crewName, deptName);
            }

        }


        return checkNameUniqueContent.TYPE_NAME_NOT_UNIQUE;//放行
    }

    //    /eidt 修改保存
    @PostMapping("/eidt")
    @RequiresPermissions("hcy:orderManger:edit")
    @ResponseBody
    public AjaxResult editSave(ProjectVo projectVo) {
        String format = getTimeFormat(projectVo, "yyyy-MM-dd hh:mm:ss");
        projectVo.setLocaldateTimeString(format);
        return toAjax(OrderManagerService.editSave(projectVo));
    }



    /*timeFormat*/
    private String getTimeFormat(ProjectVo projectVo, String s) {
        return projectVo.getProjectEndtime().format(DateTimeFormatter.ofPattern(s));
    }



    @PostMapping("/remove")
    @RequiresPermissions("hcy:orderManger:delete")
    @ResponseBody
    public AjaxResult editSave(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        return toAjax(OrderManagerService.deleteProjects(ids));
    }

}
